﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using IWshRuntimeLibrary;
using System.Diagnostics;

namespace SetEnv
{
    public partial class EnvForm : Form
    {
        private SetBoxEnv boxEnv;

        public EnvForm()
        {
            InitializeComponent();
           
            EnvNotifyIcon.Visible = true;

        }

        private void cleanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("CLEAN"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void oTC1ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("OTC1"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void oTC2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("OTC2"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void oTC3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("OTC3"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void eEDOGPPEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("PPE"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void pRODToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("PROD"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void aTC1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("ATC1"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void aTC2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("ATC2"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void aTC3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("ATC3"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void EnvForm_MinimumSizeChanged(object sender, EventArgs e)
        {
            this.Hide();
            this.Visible = false;
        }

        private void closedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Did you want to exit?", "SetEnv", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                EnvNotifyIcon.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        protected override void SetVisibleCore(bool value)
        {
            base.SetVisibleCore(value);
        }

        private void EnvNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SetBalloonTip();
        }

        private void EnvForm_Load(object sender, EventArgs e)
        {
            checkDupApp();
            SetBalloonTip();
            OnActivated(e);
            ShortcutAndStartup();
        }

        #region private mathod
        private void SetBalloonTip()
        {
            EnvNotifyIcon.BalloonTipTitle = "Setting Box Env";
            EnvNotifyIcon.BalloonTipText = "Right click to select Env.";
            EnvNotifyIcon.BalloonTipIcon = ToolTipIcon.Info;

            EnvNotifyIcon.Visible = true;
            EnvNotifyIcon.ShowBalloonTip(3000);
        }

        private void SetFailBalloonTip()
        {
            EnvNotifyIcon.BalloonTipTitle = "Setting Box Env";
            EnvNotifyIcon.BalloonTipText = "Failed to set Env!";
            EnvNotifyIcon.BalloonTipIcon = ToolTipIcon.Error;

            EnvNotifyIcon.Visible = true;
            EnvNotifyIcon.ShowBalloonTip(3000);
        }

        private void SetComplatedBalloonTip()
        {
            EnvNotifyIcon.BalloonTipTitle = "Setting Box Env";
            EnvNotifyIcon.BalloonTipText = "Complete to set IE configuration and Host!";
            EnvNotifyIcon.BalloonTipIcon = ToolTipIcon.Info;

            EnvNotifyIcon.Visible = true;
            EnvNotifyIcon.ShowBalloonTip(3000);
        }
        private bool windowCreate = true;
        protected override void OnActivated(EventArgs e)
        {

            if (windowCreate)
            {
                base.Visible = false;
                windowCreate = false;
            }
            base.OnActivated(e);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            boxEnv = new SetBoxEnv();
            if (boxEnv.SetEnv("OTHER"))
            {
                SetComplatedBalloonTip();
            }
            else
            {
                SetFailBalloonTip();
            }
        }

        private void ShortcutAndStartup()
        {
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            FileInfo fileDesktop = new FileInfo(desktopPath + "\\SetEnv.lnk");
            
            if (!fileDesktop.Exists)
            {
                WshShell shell = new WshShell();
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
                      Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                      "\\SetEnv.lnk");

                shortcut.TargetPath = Application.StartupPath + "\\" + "SetEnv.exe";
                shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
                shortcut.WindowStyle = 1;
                shortcut.Description = "Set BOX env";
                shortcut.IconLocation = Application.ExecutablePath;
                shortcut.Save();
            }
        }

        private void checkDupApp()
        {
          Process [] arrProces = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
          if (arrProces.Length>1)
          {
              MessageBox.Show("Existing!");
              this.Close();
              this.Dispose();
              GC.Collect();
          }
        }
        #endregion
    }
}
